package cn.demo.client.api;


import cn.demo.cm.domain.*;
import cn.demo.cm.service.*;
import cn.demo.common.core.controller.BaseController;
import cn.demo.common.core.domain.R;
import cn.demo.common.core.page.PageQuery;
import cn.demo.common.core.page.PageR;
import cn.demo.common.utils.CollectionUtils;
import cn.demo.common.utils.SecurityUtils;
import cn.demo.common.utils.StringUtils;
import cn.demo.um.domain.UmContentFocus;
import cn.demo.um.service.IUmContentFocusService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author xtjun
 * @description 内容api
 * @date 2024/06/04
 */
@Tag(name = "内容api")
@RestController
@RequestMapping("/api/cm")
public class CmApi extends BaseController {
    @Resource
    ICmCarouselImgService cmCarouselImgService;
    @Resource
    ICmCategoryService categoryService;
    @Resource
    ICmContentService cmContentService;
    @Resource
    IUmContentFocusService umContentFocusService;
    @Resource
    ICmFeedbackService feedbackService;
    @Resource
    ICmComplaintService cmComplaintService;

    @Operation(summary = "查询轮播图列表")
    @GetMapping("/carouselImgList")
    public R<List<CmCarouselImg>> list(CmCarouselImg cmCarouselImg) {
        return R.ok(cmCarouselImgService.selectCmCarouselImgList(cmCarouselImg));
    }

    @Operation(summary = "查询栏目列表")
    @GetMapping("/categoryList")
    public R<List<CmCategory>> list(CmCategory cmCategory) {
        List<CmCategory> list = categoryService.selectCmCategoryList(cmCategory);
        return R.ok(list);
    }

    @Operation(summary = "查询文章列表")
    @GetMapping("/contentList")
    public PageR<CmContent> contentList(CmContent cmContent, PageQuery pageQuery) {
        Page<CmContent> page = cmContentService.selectCmContentPage(cmContent, pageQuery);
        if (CollectionUtils.isNotEmpty(page.getRecords()) && null != cmContent.getParams().get("queryFocus") && StringUtils.isNotEmpty(cmContent.getParams().get("queryFocus").toString())) {
            Set<String> cIds = page.getRecords().stream().map(CmContent::getId).collect(Collectors.toSet());
            List<UmContentFocus> list = umContentFocusService.list(QueryWrapper.create().eq(UmContentFocus::getUserId, SecurityUtils.getUserId()).in(UmContentFocus::getContentId, cIds));
            Map<String, String> map = list.stream().collect(Collectors.toMap(UmContentFocus::getContentId, UmContentFocus::getUserId));
            for (CmContent record : page.getRecords()) {
                record.setIsFocus(StringUtils.isNotEmpty(map.get(record.getId())));
            }
        }
        return PageR.ok(page);
    }

    @Operation(summary = "查询文章详情")
    @GetMapping("/contentDetail")
    public R<CmContent> contentDetail(String id) {
        CmContent cmContent = cmContentService.selectCmContentById(id);
        cmContentService.addViewHit(id);
        return R.ok(cmContent);
    }

    @Operation(summary = "添加反馈")
    @PutMapping("/addFeedback")
    public R addFeedback(@RequestBody @Validated CmFeedback cmFeedback) {
        cmFeedback.setUserId(SecurityUtils.getUserId());
        cmFeedback.setIsShow(0);
        cmFeedback.setIsHandle(0);
        cmFeedback.setDeptId(SecurityUtils.getDeptId());
        return toAjax(feedbackService.insertCmFeedback(cmFeedback));
    }

    @Operation(summary = "查询推荐反馈")
    @GetMapping("/getShowFeedback")
    public PageR<CmFeedback> getShowFeedback(PageQuery pageQuery) {
        Page<CmFeedback> page = feedbackService.page(pageQuery.build(), QueryWrapper.create().eq(CmFeedback::getIsShow, 1).orderBy(CmFeedback::getCreateTime,false));
        return PageR.ok(page);
    }

    @Operation(summary = "添加举报")
    @PutMapping("/addComplaint")
    public R addFeedback(@RequestBody @Validated CmComplaint cmComplaint) {
        cmComplaint.setUserId(SecurityUtils.getUserId());
        cmComplaint.setIsHandle(0);
        cmComplaint.setDeptId(SecurityUtils.getDeptId());
        return toAjax(cmComplaintService.insertCmComplaint(cmComplaint));
    }

    @Operation(summary = "更改图片")
    @PostMapping("/changePic")
    public R changePic(@RequestBody @Validated CmComplaint cmComplaint) {
        cmComplaint.setUserId(SecurityUtils.getUserId());
        cmComplaint.setIsHandle(0);
        cmComplaint.setDeptId(SecurityUtils.getDeptId());
        return toAjax(cmComplaintService.insertCmComplaint(cmComplaint));
    }

}
